package com.ysm.smfu.food;

import android.content.SharedPreferences;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ysm.smfu.R;
import com.ysm.smfu.activity.BaseActivity;
import com.ysm.smfu.adapter.FoodCategorySellerListAdapter;
import com.ysm.smfu.bean.FoodStore;
import com.ysm.smfu.bean.KeyValueBean;
import com.ysm.smfu.food.presenter.FoodMainPersenterImp;
import com.ysm.smfu.food.view.FoodMainView;
import com.ysm.smfu.map.BasicMapActivity;
import com.ysm.smfu.map.LocationTask;
import com.ysm.smfu.map.OnLocationGetListener;
import com.ysm.smfu.map.PositionEntity;
import com.ysm.smfu.utils.UIUtils;
import com.ysm.smfu.view.CustomLoadMoreView;
import com.ysm.smfu.view.popu.ExpandPopTabView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/3/28.
 */
public class FoodCategoryActivity extends BaseActivity implements FoodMainView {

    //    智能排序
    List<KeyValueBean> smartData = new ArrayList<>();
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.map)
    ImageView map;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private FoodMainPersenterImp persenterImp;
    private SharedPreferences sp;
    private FoodCategorySellerListAdapter adapter;
    private ExpandPopTabView expandtab_view;
    private ImageView flushLoc;
    private TextView accurateAddress;
    private View animV;
    private RotateAnimation rotateAnimation;

    @Override
    protected void initViews() {
        super.initViews();
        setContentView(R.layout.activity_food_category);
        ButterKnife.bind(this);
        persenterImp = new FoodMainPersenterImp(this);
        //        显示原始数据
        persenterImp.showOriginalData();
        addToggleButton();
        persenterImp.addPopuView(this);
        flushLoc();
    }

    @Override
    public void showOriginalPage(List<FoodStore> foodStoreList) {
        initRecyclerView(foodStoreList);
    }

    @Override
    public void flushPage(List<FoodStore> foodStoreList) {
        if (adapter != null) {
            adapter.setNewData(foodStoreList);
            swipeRefresh.setRefreshing(false);
        }
    }

    @Override
    public void loadMorePageSuccess(List<FoodStore> foodStoreList) {
        if (adapter != null) {
            adapter.addData(foodStoreList);
            adapter.loadMoreComplete();
        }
    }

    @Override
    public void loadMorePageFail() {
        adapter.loadMoreFail();
    }

    @Override
    public void noMoreData() {
        adapter.loadMoreEnd();
    }

    @Override
    public void initRecommed(List<String> urls) {

    }

    @Override
    public void addItemToPopuView(List<KeyValueBean> smartData, List<KeyValueBean> listAll, List<KeyValueBean> mParentLists, List<ArrayList<KeyValueBean>> mChildrenListLists) {
        expandtab_view.addItemThree(listAll, "全部", "全部");
        expandtab_view.addItemOne(smartData, "智能排序", "智能排序");
        expandtab_view.addItemFour("筛选");
        expandtab_view.addItemTwo(mParentLists, mChildrenListLists, "锦江区", "合江亭", "附近");
    }

    String[] toggleButtonName = {"全部", "智能排序", "筛选", "附近"};

    @Override
    public void addToggleButton() {
        expandtab_view.addToggleButton(4, toggleButtonName);
    }

    /**
     * 初始化recyclerView
     */
    private void initRecyclerView(List<FoodStore> foodStoreList) {
        initAdapter(foodStoreList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter);
    }

    /**
     * 初始化adapter
     */
    private void initAdapter(List<FoodStore> foodStoreList) {
        adapter = new FoodCategorySellerListAdapter(foodStoreList);
        View view = LayoutInflater.from(this).inflate(R.layout.head_foodcategory, null);
        adapter.addHeaderView(view, 0);
        adapter.setLoadMoreView(new CustomLoadMoreView());
//        自定义加载更多
        initView(view);
        initListener();
    }

    private void initView(View view) {
//        条件选择
        expandtab_view = (ExpandPopTabView) view.findViewById(R.id.expandtab_view);
//        刷新位置
        flushLoc = (ImageView) view.findViewById(R.id.flushLoc);
//        精确位置
        accurateAddress = (TextView) view.findViewById(R.id.accurateAddress);
    }

    protected void initListener() {
        back.setOnClickListener(this);
        flushLoc.setOnClickListener(this);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                共享动画
                ImageView img = (ImageView) view.findViewById(R.id.img);
                UIUtils.startActivity(FoodDetailActivity.class);
            }
        });

        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if (adapter.getItemCount() > 10)
                    persenterImp.loadMoreData();
            }
        }, recyclerView);

        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                persenterImp.flushData();
            }
        });
    }

    @OnClick(R.id.map)
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.flushLoc://刷新位置
                animV = v;
                doAnimator(v);
                flushLoc();
                break;
            case R.id.foodMainTopBar:
                UIUtils.startActivity(FoodCategoryActivity.class);
                expandtab_view.onDismissForOther();
                break;
            case R.id.back:
                finish();
                break;
            case R.id.map:
                UIUtils.startActivity(BasicMapActivity.class);
                break;
        }
    }

    private void flushLoc() {
        final LocationTask locationTask = LocationTask.getInstance();
        locationTask.setOnLocationGetListener(new OnLocationGetListener() {
            @Override
            public void onLocationGet(PositionEntity entity) {
                if (entity != null)
                    accurateAddress.setText(String.format(UIUtils.getString(R.string.currentLoc), entity.address));
                else
                    accurateAddress.setText("定位失败");
                stopAnim();
                locationTask.stopLocate();
            }

            @Override
            public void onRegecodeGet(PositionEntity entity) {

            }
        });
        locationTask.startSingleLocate();
    }

    /**
     * 做刷新动画
     *
     * @param v
     */
    private void doAnimator(View v) {
        if (rotateAnimation == null) {
            rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            rotateAnimation.setDuration(1000);
            rotateAnimation.setRepeatCount(Animation.INFINITE);
        }
        v.startAnimation(rotateAnimation);
    }

    /**
     * 停止动画
     */
    private void stopAnim() {
        if (rotateAnimation != null) {
            rotateAnimation.cancel();
            animV.clearAnimation();
        }
    }
}
